import socket
import pyautogui as gui
import os
import startup
from  tkinter import  messagebox


startup.startup()



def get_local_ip():
    try:
        # 创建一个UDP套接字
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(("8.8.8.8", 80))  # 连接到一个公共的IP地址，这里使用Google的DNS服务器地址
        local_ip = s.getsockname()[0]  # 获取本地IP地址
        s.close()
        return local_ip
    except socket.error as exy:
        messagebox.showerror("X", str(exy))
        # print("获取本地IP地址失败:", exy)
        return None
IPI=get_local_ip()
messagebox.showinfo("IP",'本机IP为'+get_local_ip()+"\n"+"请在手机端，顶部输入IP并点击保存")
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  # 创建一个 socket 对象
server_socket.bind((IPI, 8081))  # 绑定 IP 地址和端口
server_socket.listen(5)  # 监听连接请求
print("服务器已启动，等待连接...")
while True:
    # 接受客户端连接
    client_socket, client_address = server_socket.accept()
    data = client_socket.recv(1024).decode('utf-8')
    # print(f"收到数据：{data}")
    comm = data.strip()
    if comm == "up":
        x, y = gui.position()  # 2 4
        gui.moveRel(xOffset=0, yOffset=-10)  # 相对于当前位置移动
    elif comm == "down":
        gui.moveRel(xOffset=0, yOffset=10)
    elif comm == "left":
        gui.moveRel(xOffset=-10, yOffset=0)
    elif comm == "right":
        gui.moveRel(xOffset=10, yOffset=0)
    elif comm == "mouse_l":
        a, b = gui.position()
        gui.click(a, b, button='left')  # 可以指定左键或右键
    elif comm == "mouse_r":
        a, b = gui.position()
        gui.click(a, b, button='right')  # 可以指定左键或右键
    elif comm == "taskmgr":
        os.system("taskmgr")
    elif comm == "reboot":
        os.system("shutdown /r /t 10")
    elif comm == "shutdown":
        os.system("shutdown /s /t 10")
    elif comm == "shuangji":
        a, b = gui.position()
        gui.doubleClick(a, b)
    elif comm == "danji":
        a, b = gui.position()
        gui.click(a, b)
    elif comm == "browser":
         os.system("start  https://cn.bing.com/search?q=")
    elif comm == "unable":
         os.system("shutdown  /a")

       
    elif "system" in comm:
        s = comm
        m_index = s.index('m') + 1  # 找到'm'的位置，并且加1，因为我们想要从'm'的后面开始
        result = s[m_index:]  # 从'm'的后面开始切片到字符串的结束
        print(result)
        os.system(result)

    client_socket.close()
